package cn.tedu._02notice.controler;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.dto.UserLoginParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("v1/notice")
@Tag(name = "01-通知管理模块",description="通知管理相关接口")

public class NoticeControler {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/add")
    @Operation(summary = "添加通知")
    @ApiOperationSupport (order = 10)
    public JsonResult addNotice(NoticeAddParam noticeAddParam) {
        log.debug("添加资讯："+noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);

        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("添加数据库:"+notice);
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @Operation(summary = "查询通知列表")
    @ApiOperationSupport (order = 20)
    public JsonResult listNotice(NoticeListQuery noticeListQuery) {
        log.debug("noticeListQuery = " + noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList(noticeListQuery);
        log.debug("noticeListVOS = " + noticeListVOS);
        return JsonResult.ok(noticeListVOS);
    }

    @GetMapping("/detail")
    @Operation(summary = "查询通知详情")
    @ApiOperationSupport (order = 30)
    @Parameters(value = {
            @Parameter(name = "id",description = "通知id",required = true,example="1"),
            @Parameter(name = "username",description = "用户名",required = true,example="admin",hidden=true)
    })
    public JsonResult detailNotice(Long id,String username) {
        //username无实际作用
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detailNotice(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }
    @DeleteMapping("/del")
    @Operation(summary = "删除通知")
    @ApiOperationSupport (order = 40)
    @Parameter(name = "id",description = "通知id",required = true,example="1")
    public JsonResult deleteNotice(@RequestParam Long id) {
        log.debug("id = " + id);
        int i=noticeMapper.deleteNoticeById(id);
        if(i>0){
            return new JsonResult(StatusCode.OPERATTION_SUCCESS);
        }
        return JsonResult.ok();

    }

    @PostMapping("/update")
    @Operation(summary = "修改通知")
    @ApiOperationSupport (order = 50)
    public JsonResult updateNotice(NoticeUpdateParam noticeUpdateParam) {
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int i=noticeMapper.updateNotice(notice);
        if(i>0){
            return JsonResult.ok();
        }
        return JsonResult.ok();
    }
   /* @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("userLoginParam = " + userLoginParam);
        return JsonResult.ok();
    }*/
}
